<?php
require './include/global.php';
require './include/validate.php';
require './include/all_left.php';

require_once './include/advisory_db.php';

//print_r($_SESSION);
$smarty->compile_check = true;

$smarty->display('header.tpl');

$job_id = $_GET[id];

///del

if($_GET[action]=='del'){
	  switch ($_GET[type]){
           case submitted:
			   if(!empty($_POST[del])){
					  foreach($_POST[del] as $value){
						 $str[] = "`app_id` = '".$value."' ";
					  }
					  $where = @implode(' OR ',$str);

					  $sql = "UPDATE pa_applicant SET app_isdelete = 3 WHERE ($where) AND `app_recruitid`=$_SESSION[uid] AND app_isdelete = 1 ";
				      $DB->query($sql);
					  $sql = "UPDATE pa_applicant SET app_isdelete = 2 WHERE ($where) AND `app_recruitid`=$_SESSION[uid] AND app_isdelete = 0 ";
				      $DB->query($sql);
				}
			   echo "<meta http-equiv=\"refresh\" content=\"0;URL=applicantlist.php?id=$job_id\">";
			   exit;
		   case received:
				if(!empty($_POST[del])){
					  foreach($_POST[del] as $value){
						 $str[] = "`app_id` = '".$value."' ";
					  }
					  $where = @implode(' OR ',$str);
					  $sql = "UPDATE pa_applicant SET app_isdelete = 3 WHERE ($where) AND `app_ownid`=$_SESSION[uid] AND `app_isdelete` = 2 ";
				      $DB->query($sql);
					  
					  $sql = "UPDATE pa_applicant SET app_isdelete = 1 WHERE ($where) AND `app_ownid`=$_SESSION[uid] AND `app_isdelete` = 0 ";
				      $DB->query($sql);
				}
				echo "<meta http-equiv=\"refresh\" content=\"0;URL=applicantlist.php?id=$job_id\">";
			   exit;
	  }
}
//check type
$sql      =   "SELECT * FROM pa_job WHERE jo_id = '".$job_id."' AND jo_uid = '".$_SESSION[uid]."' AND jo_utype ='".$_SESSION[usertype]."'";
$rs       =   $DB->fetch_one_array($sql);
$type     =  (empty($rs))?"Submitted":"Received";
$owner    =  (empty($rs))?" AND `app_recruitid` = '".$_SESSION[uid]."'":"";
$isdelete =  (empty($rs))?" AND (a.app_isdelete = 0 OR a.app_isdelete = 1)  ":" AND (a.app_isdelete = 0 OR a.app_isdelete = 2) ";


//connect with recruiter
if($_GET[action]=="connect"){
   $sql = "UPDATE pa_applicant SET app_submittime = '".time()."' WHERE app_id='".$_GET[app_id]."'";
   $DB->query($sql);
   echo "<meta http-equiv=\"refresh\" content=\"0;URL=applicantlist.php?id=".$_GET[id]."\">";
   exit;
}
//fill the job date or salary detail
if($_GET[action]=="Enter"){
    $sql = "UPDATE pa_applicant SET `app_hired` = '1' ,`app_offerdate`='".trim($_POST[offerdate])."', `app_onboarddate`='".trim($_POST[onboarddate])."',`app_annualsalary`='".trim($_POST[offersalary])."' WHERE app_id = '".$_POST[app_id]."' ";
	$DB->query($sql);

	$sql2 = "SELECT a.*,b.re_fname,b.re_lname,b.re_yearexp,b.re_edulevel,b.re_desiredpay,b.re_prvemployer,b.re_desiredpay,b.re_lostate,b.re_locountry,b.re_yearexp,c.rc_uname,d.jo_id,d.jo_title,d.jo_vdate,d.jo_copname,d.jo_status  FROM pa_applicant as a ,pa_resume as b,pa_recuiter as c,pa_job as d  WHERE d.jo_id = a.app_jobid  AND a.app_resumeid = b.re_id AND c.rc_id = a.app_recruitid  AND a.app_id = '".$_POST[app_id]."'";
	$list2 = $DB->fetch_one_array($sql2);

	$subject  = "Candidate Hired";
	$content  = "
				  Job      Req#      :  ".$list2[jo_id]."  <br>  
				  Employer  Username :  ".$_SESSION[uname]."<br>
				  Applicant Name     :  ".$list2[re_fname]." ".$list2[re_lname]."<br>
				  Recruiter Username :  ".$list2[rc_uname]."<br> 
			    ";
	$headers .= "From: enetRecruiter Support <support@enetrecruiter.com>" . "\r\n";
	$headers .= "Content-type: text/plain; charset=utf-8" . "\r\n";
    $to       = "accounting@enetrecruiter.com,zeelaura@yahoo.com";
	@mail($to, $subject, $content, $headers);
    exit;
}

//search result
$smarty->assign('state',$state);
$smarty->assign('country',$country);
if(trim($_GET[action]) == 'search'){
    $arr = array();
	if(trim($_GET['keywords'])!=''){ $arr[] = " b.re_detail  like '%".trim($_GET[keywords])."%' ";}	
	if(trim($_GET['date']) !='' ){ $arr[] = " FROM_UNIXTIME(a.app_regtime,'%m/%d/%Y') = '".trim($_GET['date'])."' ";}	
    if(trim($_GET['name']) != '' ){ $arr[] = " (
	( a.app_recruitid >0 AND b.re_nickname like '%".trim($_GET['name'])."%') 
	OR  
	( a.app_candidateid >0 AND ( b.re_fname like '%".trim($_GET['name'])."%'
		OR b.re_lname like '%".trim($_GET['name'])."%')
	))";}	
	if(trim($_GET['location'])!=''){ $arr[] = " ( b.re_address1  like '%".trim($_GET['location'])."%' OR b.re_locity  like '%".trim($_GET['location'])."%' ) ";}	
	if(trim($_GET['recruiter'])!=''){ 
		$sql = "SELECT rc_id FROM pa_recuiter WHERE rc_fname like '%".trim($_GET['recruiter'])."%' OR rc_lname like '%".trim($_GET['recruiter'])."%'";
		$recIds = $DB->fetch_all($sql);
		if(!empty($recIds)){
			foreach($recIds as $id){
				$arrIds[] = $id['rc_id'];
			}
			$Ids = @implode(',',$arrIds);
			$arr[] = " ( b.re_usertype = 'recruiter' AND b.re_uid IN (".$Ids.") ) ";
		} 
	}
	if($_GET['state']!='0'&&$_GET['state']!='') { $arr[] = " b.re_lostate	 = '".$_GET['state']."'  ";}
	if($_GET['country']!='0'&&$_GET['country']!='') { $arr[] = " b.re_locountry = '".$_GET['country']."'  "; }

	if(!empty($arr)){
		$implode  = @implode("AND", $arr);
		$implode  = " AND ".$implode;
		$where    .= $implode;
	}
	$smarty->assign("s",$_GET);
}


$nav = new buildNav;
$nav->limit = "25";

if($_GET[tpp] == "posted"){
	$sql = "SELECT COUNT(*) AS count FROM pa_applicant as a ,pa_resume as b,pa_job as d  WHERE  d.jo_id = a.app_jobid AND a.app_resumeid = b.re_id  AND a.app_jobid = '".$job_id."'$where $owner $isdelete";
}else{
	$sql ="SELECT COUNT(*) AS count FROM pa_applicant as a ,pa_resume as b,pa_job as d  WHERE  d.jo_id = a.app_jobid AND a.app_resumeid = b.re_id  AND a.app_jobid = '".$job_id."'$where $owner $isdelete";
}
$total = $DB->fetch_one_array($sql);

$nav->total_result = $total[count];

$by    = "app_regtime"; 
$order = "DESC" ;
if($_GET[tp]==1){
	$_GET[order1]==''&&$order1=1;$_GET[order2]==''&&$order2=1;$_GET[order3]==''&&$order3=1;$_GET[order4]==''&&$order4=1;
	$_GET[order5]==''&&$order5=1;$_GET[order6]==''&&$order6=1;$_GET[order7]==''&&$order7=1;$_GET[order8]==''&&$order8=1;$_GET[order9]==''&&$order9=1;
	switch($_GET[swh]){
	   case 1:
		   $by = "app_regtime";
		   if($_GET[order1]==1){$order_arr[1]=2; $order=" DESC ";$image_arr[1] = "<img src=\"images/dot1.gif\" align=\"absmiddle\">";}else{$order_arr[1]=1;$order=" ASC ";$image_arr[1] = "<img src=\"images/dot2.gif\"  align=\"absmiddle\">";}
		   break;
	   case 2:
		   $by = "re_fname";
		   if($_GET[order2]==1){$order_arr[2]=2; $order=" DESC ";$image_arr[2] = "<img src=\"images/dot1.gif\"  align=\"absmiddle\">";}else{$order_arr[2]=1;$order=" ASC ";$image_arr[2] = "<img src=\"images/dot2.gif\"  align=\"absmiddle\">";}
		   break;
	   case 3:
			$by = "re_lostate";
		   if($_GET[order3]==1){$order_arr[3]=2; $order=" DESC ";$image_arr[3] = "<img src=\"images/dot1.gif\"  align=\"absmiddle\">";}else{$order_arr[3]=1;$order=" ASC ";$image_arr[3] = "<img src=\"images/dot2.gif\"  align=\"absmiddle\">";}
		   break;
	   case 4:
		   $by = "jo_myrwe";
		   if($_GET[order4]==1){$order_arr[4]=2; $order=" DESC ";$image_arr[4] = "<img src=\"images/dot1.gif\"  align=\"absmiddle\">";}else{$order_arr[4]=1;$order=" ASC ";$image_arr[4] = "<img src=\"images/dot2.gif\"  align=\"absmiddle\">";}
		   break;
		case 5:
		   $by = "app_submittime";
		   if($_GET[order5]==1){$order_arr[5]=2; $order=" DESC ";$image_arr[5] = "<img src=\"images/dot1.gif\"  align=\"absmiddle\">";}else{$order_arr[5]=1;$order=" ASC ";$image_arr[5] = "<img src=\"images/dot2.gif\"  align=\"absmiddle\">";}
		   break;
	   case 6:
		   $by = "re_desiredpay";
		   if($_GET[order6]==1){$order_arr[6]=2; $order=" DESC ";$image_arr[6] = "<img src=\"images/dot1.gif\"  align=\"absmiddle\">";}else{$order_arr[6]=1;$order=" ASC ";$image_arr[6] = "<img src=\"images/dot2.gif\"  align=\"absmiddle\">";}
		   break;
	  case 7:
		   $by = "re_schoolattend";
		   if($_GET[order7]==1){$order_arr[7]=2; $order=" DESC ";$image_arr[7] = "<img src=\"images/dot1.gif\"  align=\"absmiddle\">";}else{$order_arr[7]=1;$order=" ASC ";$image_arr[7] = "<img src=\"images/dot2.gif\"  align=\"absmiddle\">";}
		   break;
	  case 8:
		   $by = "app_fee";
		   if($_GET[order8]==1){$order_arr[8]=2; $order=" DESC ";$image_arr[8] = "<img src=\"images/dot1.gif\"  align=\"absmiddle\">";}else{$order_arr[8]=1;$order=" ASC ";$image_arr[8] = "<img src=\"images/dot2.gif\"  align=\"absmiddle\">";}
		   break;
	  case 9:
		   $by = "rc_uname";
		   if($_GET[order9]==1){$order_arr[9]=2; $order=" DESC ";$image_arr[9] = "<img src=\"images/dot1.gif\"  align=\"absmiddle\">";}else{$order_arr[9]=1;$order=" ASC ";$image_arr[9] = "<img src=\"images/dot2.gif\"  align=\"absmiddle\">";}
		   break;
	  case 10:
		   $by = "jo_status";
		   if($_GET[order10]==1){$order_arr[10]=2; $order=" DESC ";$image_arr[10] = "<img src=\"images/dot1.gif\"  align=\"absmiddle\">";}else{$order_arr[10]=1;$order=" ASC ";$image_arr[10] = "<img src=\"images/dot2.gif\"  align=\"absmiddle\">";}
		   break;
	  case 11:
		   $by = "re_yearexp";
		   if($_GET[order11]==1){$order_arr[11]=2; $order=" DESC ";$image_arr[11] = "<img src=\"images/dot1.gif\"  align=\"absmiddle\">";}else{$order_arr[11]=1;$order=" ASC ";$image_arr[11] = "<img src=\"images/dot2.gif\"  align=\"absmiddle\">";}
		   break;
	  case 12:
		   $by = "re_edulevel";
		   if($_GET[order12]==1){$order_arr[12]=2; $order=" DESC ";$image_arr[12] = "<img src=\"images/dot1.gif\"  align=\"absmiddle\">";}else{$order_arr[12]=1;$order=" ASC ";$image_arr[12] = "<img src=\"images/dot2.gif\"  align=\"absmiddle\">";}
		   break;
	  case 13:
		   $by = "re_desiredpay";
		   if($_GET[order13]==1){$order_arr[13]=2; $order=" DESC ";$image_arr[13] = "<img src=\"images/dot1.gif\"  align=\"absmiddle\">";}else{$order_arr[13]=1;$order=" ASC ";$image_arr[13] = "<img src=\"images/dot2.gif\"  align=\"absmiddle\">";}
		   break;
	}
		$smarty->assign("order_arr",$order_arr);
		$smarty->assign("image_arr",$image_arr);

}

if($_GET[tpp] == "posted"){
	$sql = "SELECT * FROM pa_applicant as a ,pa_resume as b,pa_job as d  WHERE  d.jo_id = a.app_jobid AND a.app_resumeid = b.re_id  AND a.app_jobid = '".$job_id."'$where $owner $isdelete";
}else{
	$sql ="SELECT * FROM pa_applicant as a ,pa_resume as b,pa_job as d  WHERE  d.jo_id = a.app_jobid AND a.app_resumeid = b.re_id   AND a.app_jobid = '".$job_id."'$where $owner $isdelete";
}
$nav->execute($sql." ORDER BY  $by $order");

$page = $nav->pagenav();


$i=0;
while($list2 = $DB->fetch_array($nav->sql_result)){
	   $datajob[$i]                  = $list2;
	   $datajob[$i][jo_id]           = $job_id;
	   $datajob[$i][app_regtime]     = @date("m/d/Y h:i:s A",$list2[app_regtime]);
	   $datajob[$i][re_desiredpay]   = "$".number_format($datajob[$i][re_desiredpay]);
	   //added at 03-06-2012 by Terry
	   if($list2[app_owntype] == 'recruiter'){
		  $datajob[$i][name]            = $list2['re_nickname'];
		  $sql = "SELECT rc_fname, rc_lname FROM pa_recuiter WHERE rc_id='".$list2[re_uid]."'";
		  $recName = $DB->fetch_one_array($sql);
		  $datajob[$i][rec_profile]     = '<br /><a href="recdetail.php?id='.$list2[re_uid].'" target="_blank">'.$recName['rc_fname'].' '.$recName['rc_lname'].'</a>';
	   }else{
		  $datajob[$i][name]            = $list2[re_fname]." ".$list2[re_lname];
	   }
	   //added at 03-06-2012 by Terry
	   $datajob[$i][location]        = $state[$list2[re_lostate]].",".$country[$list2[re_locountry]];
	   $datajob[$i][re_yearexp]      = ($list2[re_yearexp]==0)?"N/A":$yearexp2[$list2[re_yearexp]];
	   $datajob[$i][rc_uname]        = $list2[rc_uname];
	   $datajob[$i][re_jobsearchstatus]       = $jobSearchStatus[$list2[re_jobsearchstatus]];
	   $datajob[$i][jo_status]       = $jobfillstatus[$list2[jo_status]];
	   $datajob[$i][app_status]      = ($list2[app_status]==0)?"N/A":$appstatus[$list2[app_status]];
	   $datajob[$i][jo_myrwe]      = $yearexp2[$list2[jo_myrwe]];
	   $datajob[$i][re_edulevel]     = ($list2[re_edulevel]==0)?"N/A":$education[$list2[re_edulevel]];
	   $datajob[$i][app_fee]         = number_format($list2[app_fee]);
	   $datajob[$i][re_desiredpay]   = number_format($list2[re_desiredpay]);
	   $datajob[$i][jo_vdate]        = (trim($list2[jo_vdate]) == "NA" || trim($list2[jo_vdate]) == "")? "N/A" :@date("m/d/Y",strtotime($list2[jo_vdate]));

	  
	   $i++;
}
//print_r($datajob[0]);
$smarty->assign("jobdetail",$datajob[0]);
$smarty->assign("tpp",$_GET[tpp]);

$smarty->assign("applist",$datajob);

$smarty->assign("page",$page);
if($err!=''){
$smarty->assign("err",$err);
}
$smarty->assign("job_id",$job_id);
$smarty->assign("appnum", $total[count]);
$smarty->display('allleft.tpl');

//select
if($type == "Received"){
$smarty->display('applicantlist_received.tpl');
}else{
$smarty->display('applicantlist_submitted.tpl');
}

$smarty->display('footer.tpl');
?>